<?php
// 本类由系统自动生成，仅供测试用途
import('ORG.Util.Session');//导入session类
class IndexAction extends Action {
    public function index(){
	    import("ORG.Util.YiBan");
		$cfg = array(
			'appID'		=> '357c03ae46d9465a',
			'appSecret'	=> '07726bf2dfefe1ce7d94fc8a77a8c4d9',
			'callback'	=> 'http://f.yiban.cn/iapp141991'// 站内应用这里的 callback 是管理中心里看到的“站内地址”
		);

		if(empty($_SESSION['name']))
		{
			$api = YBOpenApi::getInstance()->init($cfg['appID'], $cfg['appSecret'], $cfg['callback']);
			if (empty($_SESSION['token']))
			{
				try
				{
					/*
					 * 调用perform()验证授权，若未授权会自动重定向到授权页面
					 * 授权成功返回的数组中包含用户基本信息及访问令牌信息
					 */
					$info = $api->getFrameUtil()->perform();
					//print_r($info);	// 可以输出info数组查看
					// 访问令牌[visit_oauth][access_token]
					$_SESSION['token']	= $info['visit_oauth']['access_token'];
					$_SESSION['usrid']	= $info['visit_user']['userid'];
					$_SESSION['name']	= $info['visit_user']['username'];
				}
				catch (YBException $ex)
				{
					echo $ex->getMessage();
				}
			}
			$adaptive = $api->getFrameUtil()->adaptive();

			//获取用户数据
	        $access_token = $_SESSION['token'];
	        //获取普通权限接口
	        $uri = "https://openapi.yiban.cn/user/me?access_token={$access_token}";
	        //获取校级权限接口
	        //$uri = "https://openapi.yiban.cn/user/verify_me?access_token={$access_token}";
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
	        curl_setopt($ch, CURLOPT_USERAGENT, 'Yi OAuth2 v0.1');
	        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_ENCODING, "");
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_HEADER, false);
	        curl_setopt($ch, CURLOPT_URL, $uri);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array());
	        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
	        $response = curl_exec($ch);
	        $userInfo = json_decode($response,true);
	        var_dump($userInfo);
		}else{
			var_dump($userInfo);
		}
	}
}
